package com.education.wisdom.service.impl;

import com.education.wisdom.feign.WelcoFeign;
import com.education.wisdom.mapper.CatalogMapper;
import com.education.wisdom.mapper.ResourcesMapper;
import com.education.wisdom.mapper.UserCatalogBuyMapper;
import com.education.wisdom.pojo.Catalog;
import com.education.wisdom.pojo.UserCatalogBuy;
import com.education.wisdom.pojo.dto.MineResTableDTO;
import com.education.wisdom.service.IUserCatalogBuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.education.wisdom.pojo.consts.FileConst.*;

/**
 * 用户购买课程 服务层实现
 *
 * @author ruoyi
 * @date 2019-05-27
 */
@Service
public class UserCatalogBuyServiceImpl implements IUserCatalogBuyService {
    @Autowired
    private UserCatalogBuyMapper userCatalogBuyMapper;
    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    private ResourcesMapper resourcesMapper;
    @Autowired
    private WelcoFeign welcoFeign;

    @Override
    public UserCatalogBuy selectUserCatalogBuyByUserId(Map<String, Object> map) {
        return userCatalogBuyMapper.selectUserCatalogBuyByUserId(map);
    }

    @Override
    public List<UserCatalogBuy> selectByUserId(Map<String, Object> map) {
        return userCatalogBuyMapper.selectByUserId(map);
    }

    @Override
    public int addUserCatalogBuy(List<UserCatalogBuy> list) {
        return userCatalogBuyMapper.addUserCatalogBuy(list);
    }

    @Override
    public void insert(UserCatalogBuy userCatalogBuy) {
        userCatalogBuyMapper.insert(userCatalogBuy);
    }

    @Override
    public Integer[] selectCatalogIdByUserId(Integer userId) {
        return userCatalogBuyMapper.selectCatalogIdByUserId(userId);
    }

    @Override
    public void deleteByUserId(String userId) {
        userCatalogBuyMapper.deleteByUserId(userId);
    }


    /**
     * 专业课程体系下购买资源后同步到我的资源库
     *
     * @param catalogId 今软资源库的关联目录id
     * @param userId    购买资源的老师id
     */
    @Async
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void professionalCoursesSynchronousMineRes(Integer catalogId, String userId) {
        //    默认分组的查找 老师id和默认分组类型     图片4/视频5/文档6
        Map<String, Integer> groupMap = welcoFeign.defaultGroupQueryAndReturn(Integer.valueOf(userId));

        //查找视频
        List<Catalog> catalogs = catalogMapper.selectByParentId(catalogId);
        List<Integer> catalogIdListChild = catalogs.parallelStream().map(catalog -> catalog.getCatalogId()).collect(Collectors.toList());
        List<MineResTableDTO> videoList = resourcesMapper.selectResByType(catalogId, 4);
        List<MineResTableDTO> videoList2 = new ArrayList<>();
        if (catalogIdListChild.size() > 0) {
            //视频1.虚拟教学
            videoList2 = resourcesMapper.selectVideoByCatalogIdList(catalogIdListChild);
        }
        videoList.addAll(videoList2);
        handleUrlToSuffix(videoList, groupMap.get("5"));//格式处理
        //查找图片
        List<MineResTableDTO> imageList = resourcesMapper.selectResByType(catalogId, 5);
        handleUrlToSuffix(imageList, groupMap.get("4"));//格式
        //查找文档
        List<String> typeList = Arrays.asList("1", "2", "3");
        List<MineResTableDTO> docList = resourcesMapper.selectResByTypeList(catalogId, typeList);
        handleUrlToSuffix(docList, groupMap.get("6"));//格式


        videoList.addAll(imageList);
        videoList.addAll(docList);

        //我的资源库的资源插入  批量插入
        resourcesMapper.batchInsertMineResTable(videoList);

    }

    //url中处理出来后缀
    public void handleUrlToSuffix(List<MineResTableDTO> resList, Integer catalogId) {
        for (MineResTableDTO mineResTableDTO : resList) {
            String extension = mineResTableDTO.getMineResUrl().replace(mineResTableDTO.getMineResUrl().substring(0, mineResTableDTO.getMineResUrl().lastIndexOf(".")) + ".", "");
            mineResTableDTO.setExtension(extension);
            mineResTableDTO.setResType(judgeResType(extension));
            mineResTableDTO.setCreateBy("专业课程体系资源同步");
            mineResTableDTO.setCatalogId(catalogId);
        }
    }

    //根据文件后缀判断资源类型
    public String judgeResType(String extension) {
        //        1
        if (IMG_FILE.contains(extension)) return "1";
        //        2
        if (VIDEO_FILE.contains(extension)) return "2";
        //        3
        if (DOC_FILE.contains(extension)) return "3";
        return "0";
    }

}
